<?php

/* @var $this \yii\web\View */

/* @var $content string */

use yii\helpers\Html;

$web = \yii\helpers\Url::base();
$url = \app\librarys\Tools::urlBase();
$menuList = \app\models\Menu::getAllMenus();
$manager = \app\models\Manager::getCurrentManager();
$currentControllerId = Yii::$app->controller->id;
$currentActionId = Yii::$app->controller->action->id;

//var_dump($currentControllerId);var_dump($currentActionId);exit;
$currentMenu = [];
foreach ($menuList as $val) {
    if (!$val['parent_id'] && isset($val['sub_menu_list'])) {
        foreach ($val['sub_menu_list'] as $v) {
            if(!$v['action']){
                $menu_children = \app\models\Menu::getMenuByParentId($v['id']);
                foreach ($menu_children as $menu_child){
                    if($menu_child['action'] == $currentActionId){
                        $v['action'] = $menu_child['action'];
                        break;
                    }
                }
            }

            if ($v['controller'] == $currentControllerId && $v['action'] == $currentActionId ) {
                $currentMenu = $val;
                break;
            }
        }
        if($currentMenu){
            break;
        }
    }
}
function getUrlForNode($obj, $url)
{
    $url_for_node = '';
    if (isset($obj['sub_menu_list'])) {
        $length = count($obj['sub_menu_list']) - 1;
        $obj['sub_menu_list'] = array_values($obj['sub_menu_list']);
        for ($i = $length; $i >= 0; $i--) {
            $url_for_node = getUrlForNode($obj['sub_menu_list'][$i], $url);
        }
    } else {
        return "{$url}/{$obj['controller']}/{$obj['action']}";
    }
    return $url_for_node;
}

if ($menuList) {
    foreach ($menuList as $key => $val) {
        if (!$val['parent_id'] && isset($val['sub_menu_list'])) {
            $menuList[$key]['url_for_root'] = getUrlForNode($val, $url);
        } else {
            $menuList[$key]['url_for_root'] = "javascript:;";
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!--    <meta name="viewport" content="width=device-width, initial-scale=1">-->

    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/font-awesome.css">
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/frame.css">
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/tree-autocomplete.css">
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/jquery.Jcrop.css">

    <!--[if IE]>
    <link rel="stylesheet" type="text/css" href="<?= $web ?>/css/ie.css"/>
    <![endif]-->

    <style type="text/css">
        #errorModal {
            z-index: 1099;
        }

        #infoModal {
            z-index: 1098;
        }

        #confirmModal {
            z-index: 1097;
        }
    </style>

    <script type="text/javascript" src="<?= $web ?>/js/jquery-1.11.3.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/jquery.cookie.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/bootstrap.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/frame.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/tools.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/tree-autocomplete.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/tree-autocomplete-pager.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/bootstrap-datetimepicker.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/bootstrap-datetimepicker.zh-CN.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/validate.js"></script>
    <script type="text/javascript" src="<?= $web ?>/js/jcrop/jquery.Jcrop.js"></script>


    <script type="text/javascript">
        $(function () {
            //模态框点击取消或者X才能关闭
            $('#myModalEdit').modal({
                keyboard: false, backdrop: 'static', show: false
            });

            $('#confirmModal').modal({
                keyboard: false, backdrop: 'static', show: false
            });


            $('#editCurrentManagerInfo').on('show.bs.modal', function (e) {
                var managerId = e.relatedTarget.attributes['data-manager-id'].value;
                if (managerId == '0') {
                    $("#editCurrentManagerInfoOk").hide();
                }
                $('#editCurrentManagerInfoContent').html('正在加载数据....');

                requestHtml("<?= $url ?>/system/manager-info-detail-partial", {"id": managerId}, true, function (responseHtml) {
                    $("#editCurrentManagerInfoContent").html(responseHtml);

                });
            });

            $('#editCurrentManagerInfoOk').click(function () {
                var id = $("#editCurrentManagerInfoContent").find("#id").val();
                var name = $("#editCurrentManagerInfoContent").find("#name").val().trim();
                var phone = $("#editCurrentManagerInfoContent").find("#phone").val().trim();
                var remark = $("#editCurrentManagerInfoContent").find("#remark").val().trim();


                if (name == '' || name == null) {
                    errorDialog('名称不能为空');
                    return false;
                }

                if (validateLength(name, '名称', 1, 254) == false) {
                    return false;
                }

                //验证账号的唯一性

                if (validatePhone(phone) == false) {
                    errorDialog('电话号码不规范');
                    return false;
                }

                if (validateLength(remark, '备注', '1', '254', 1) == false) {
                    return false;
                }

                var data = {};
                data.id = id;
                data.name = name;
                data.phone = phone;
                data.remark = remark;
                requestJson("<?= $url ?>/system/manager-info-save-handler", data, true, function (response) {
                    if (response.status == 0) {
                        window.location.reload();
                    } else {
                        errorDialog(response.message);
                    }
                });
            })
        });

        function logOut() {
            confirmDialog(null, '确认退出？', function () {
                window.location.href = "<?= $url?>/site/signout";
            });
        }

        function saveCurrentManagerPassword() {
            var old_password = $("#editCurrentManagerPasswordContent").find("#old_password").val().trim()
            var password = $("#editCurrentManagerPasswordContent").find("#password").val().trim();
            var repassword = $("#editCurrentManagerPasswordContent").find("#repassword").val().trim();

            if (old_password == '' || old_password == null) {
                errorDialog('旧密码密码不能为空');
                return false;
            }

            if (password == '' || password == null) {
                errorDialog('新密码不能为空');
                return false;
            }


            if (repassword == '' || repassword == null) {
                errorDialog('重复密码不能为空');
                return false;
            }

            if (repassword != password) {
                errorDialog('重复密码和新密码不一致');
                return false;
            }

            if ((validateLength(repassword, '密码', 6, 16) == false)) {
                errorDialog('密码长度在6到16位之间');
                return false;
            }


            var data = {};
            data.id = "<?= $manager['id']?>";
            data.password = password;
            data.old_password = old_password;
            requestJson("<?= $url ?>/system/manager-info-save-password-handler", data, true, function (response) {
                if (response.status == 0) {
                    filter();
                } else {
                    errorDialog(response.message);
                }
            });
        }
    </script>

    <title><?= Html::encode($this->title) ?></title>
</head>
<body>
<div class="main">
    <!--头部-->
    <div class="header">
        <div class="header-logo"></div>
        <div class="header-v">版本<span>V1.0.0</span></div>
        <div class="header-userMain">
            <div class="header-userMain-item" style="display: none;">
                <img class="userMain-item-icon" src="<?= $web ?>/images/header-clean.png" alt="">
                <span class="userMain-item-disc">清理缓存</span>
            </div>
            <hr>
            <div class="header-userMain-item" style="display: none;" onclick="logOut();">
                <img class="userMain-item-icon" src="<?= $web ?>/images/header-exit.png" alt="">
                <span class="userMain-item-disc">安全退出</span>
            </div>
            <div class="header-userMain-item">
                <div class="dropdown">
                    <div class=" dropdown-toggle" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true"
                         aria-expanded="true">
                        <img class="userMain-item-icon" src="<?= $web ?>/images/header-manager.png" alt="">
                        <span class="userMain-item-disc"><?= isset($manager['name']) ? $manager['name'] : '' ?></span>
                        <span class="caret"></span>
                    </div>
                    <ul class="dropdown-menu manager-menu" aria-labelledby="dropdownMenu1">
                        <li><a href="#editCurrentManagerInfo" data-manager-id="<?= $manager['id'] ?>"
                               data-toggle="modal">修改个人信息</a></li>
                        <li><a href="#editCurrentManagerPassword" data-manager-id="<?= $manager['id'] ?>"
                               data-toggle="modal">修改密码</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="javascript:logOut();">安全退出</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <!--左边导航-->
    <div class="nav-left">
        <div class="nav-wrapper">
            <div class="nav-date"><?= '星期' . mb_substr("日一二三四五六", date("w"), 1, "utf-8") ?>
                &nbsp;<?= date('Y-m-d', time()) ?></div>
            <div class="nav-content">
                <div class="nav-content-left">
                    <div class="nav-switch">
                        <i class="nav-left-icon"></i>
                    </div>

                    <?php
                    $menu_status = \Yii::$app->session->get(\app\models\Menu::SessionMenuStatus)['status'];
                    if ($menu_status == null) {
                        $wrapper_width = '100%';
                    } else if ($menu_status == 1) {
                        $wrapper_width = '38px';
                    } else {
                        $wrapper_width = '100%';
                    }
                    ?>

                    <!-- 一级 -->
                    <div class="nav-list-wrapper" style="width: <?= $wrapper_width ?>">
                        <?php
                        if ($menuList) {
                            foreach ($menuList as $val) {
//                                var_dump($currentMenu);
                                if (!$val['parent_id']) {
                                    ?>
                                    <div
                                            class="nav-list <?= $val['icon'] ?> <?php if ($currentMenu['name'] == $val['name']) { ?>nav-list-active<?php } ?>"
                                            data-toggle="tooltip" data-placement="right"
                                            title="<?= $val['name'] ?>"
                                            data-toggle-title="<?= $val['name'] ?>"
                                            data-tag="goods-editor"
                                            onclick="window.location.href='<?= $val['url_for_root'] ?>';">
                                        <i class="nav-left-icon"
                                           onclick="window.location.href='<?= $val['url_for_root'] ?>';"></i>
                                        <span class="nav-list-text"
                                              onclick="window.location.href='<?= $val['url_for_root'] ?>';"><?= $val['name'] ?></span>
                                    </div>
                                <?php }
                            }
                        } ?>
                    </div>
                </div>
                <div class="nav-list-alert">
                </div>
                <div class="nav-content-right">
                    <div class="nav-content-right-wrapper">
                        <!-- 子级开始 -->
                        <?php
                        if ($currentMenu) {
                            foreach ($currentMenu['sub_menu_list'] as $key => $val) {
                                $url_for_node = "javascript:;";
                                if (!isset($val['sub_menu_list'])) $url_for_node = getUrlForNode($val, $url);
                                ?>
                                <div class="nav-menu-item  menu-wrapper">

                                    <a href="<?= $url_for_node ?>"
                                       class="nav-menu nav-second-menu <?php if ($url_for_node != "javascript:;" && $val['action'] == $currentActionId) { ?>nav-menu-active<?php } ?>"
                                       id="<?= $val['controller'] ?>_<?= $key ?>">
                                        <i class="menu-icon<?php if ($val['sub_menu_list']) { ?> second-menu-icon<?php } ?>"></i>
                                        <span><?= $val['name'] ?><!--（二级）--></span>
                                    </a>
                                    <div class="nav-fourth-menu-wrapper menu-wrapper">
                                        <?php
                                        if (isset($val['sub_menu_list'])) {
                                            foreach ($val['sub_menu_list'] as $k => $v) {
                                                $url_for_node_third = "javascript:;";
                                                if (!$v['sub_menu_list']) $url_for_node_third = getUrlForNode($v, $url);
                                                ?>
                                                <a href="<?= $url_for_node_third ?>"
                                                   class="nav-menu nav-third-memu open "
                                                   id="<?= $v['controller'] ?>_3_<?= $key ?>_<?= $k ?>">
                                                    <i class="menu-icon<?php if ($v['sub_menu_list']) { ?> third-menu-icon<?php } ?>"></i>
                                                    <span><?= $v['name'] ?><!--（三级）--></span>
                                                </a>
                                                <script>
                                                    if ("<?= $v['action']?>" == "<?= $currentActionId?>") {
                                                        $("#<?= $val['controller']?>_<?= $key?>").addClass('active open');
                                                        $("#<?= $v['controller']?>_3_<?= $key?>_<?= $k?>").addClass('nav-menu-active');
                                                    }
                                                </script>
                                            <?php
                                            if ($v['sub_menu_list']){
                                            foreach ($v['sub_menu_list'] as $v4){
                                            $url_for_node_fourth = "javascript:;";
                                            if (!$v4['sub_menu_list']) $url_for_node_fourth = getUrlForNode($v4, $url);
                                            ?>
                                                <a href="<?= $url_for_node_fourth ?>" class="nav-menu nav-fourth-menu">
                                                    <?= $v4['name'] ?><!--（四级）-->
                                                </a>
                                                <script>
                                                    if ("<?= $v4['action']?>" == "<?= $currentActionId?>") $("#<?= $v['controller']?>_3_<?= $key?>_<?= $k?>").addClass('active');
                                                </script>
                                            <?php }
                                            }
                                            }
                                        } ?>
                                    </div>

                                </div>
                            <?php }
                        } ?>
                        <!-- 子级结束 -->
                    </div>
                    <div class="nav-content-right-wrapper-blank"></div>
                </div>
            </div>

        </div>
        <div class="zw"></div>
    </div>
    <script>
        $(document).ready(function () {
            $(window).resize(function () {
                setContentMargin();
            });

            setNavListTitle();

            // 左边导航栏折叠
            $('.nav-switch').click(function () {
                // $(".nav-list-wrapper").animate({width:"38px"})
                if ($(".nav-list-wrapper").width() > 38) {
                    $(".nav-list-wrapper").animate({width: "38px"}, 100, function () {
                        setNavListTitle();
                        setContentMargin();
                        modifyMenuStatus();
                    });

                } else {
                    $(".nav-list-wrapper").animate({width: "100%"}, 100, function () {
                        setNavListTitle();
                        setContentMargin();
                        modifyMenuStatus();
                    });
                }
            });

        });

        function setContentMargin() {
            var navLeftWid = $(".nav-left").width();
            $('.main-content').css("margin-left", navLeftWid);
        }

        function modifyMenuStatus() {
            var data = {};
            data.content_width = $(".nav-left").width();
            requestHtml("../system/modify-menu-status", data, true, function (response) {
            });
        }
    </script>
    <!--主页-->
    <?php
    $content_width = \Yii::$app->session->get(\app\models\Menu::SessionMenuStatus)['content_width'];
    if ($content_width == null) {
        $content_width = '285px';
    }
    $content_width_style = 'style="margin-left: ' . $content_width . 'px"';
    //    $content_width_style = isset($content_width) ? 'style="margin-left: ' . $content_width . 'px"' : '';
    ?>
    <div class="main-content" <?= $content_width_style ?> >
        <?= $content ?>
    </div>
</div>

<!-- 修改个人信息 -->
<div class="modal fade" id="editCurrentManagerInfo" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">修改个人信息</h4>
            </div>
            <div id="editCurrentManagerInfoContent" class="modal-body">
            </div>
            <div class="modal-footer">

                <a class="btn btn-primary" id="editCurrentManagerInfoOk"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>

<!-- 修改密码 -->
<div class="modal fade" id="editCurrentManagerPassword" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">修改密码</h4>
            </div>
            <div id="editCurrentManagerPasswordContent" class="modal-body">

                <div class="form-horizontal" id="manager_edit_form">
                    <div class="form-group" style=" margin-top: 5px">
                        <label class="control-label col-lg-2" for="old_password">旧密码：</label>
                        <div class="col-lg-10">
                            <input type="password" style="display: none">
                            <input type="password" id="old_password" name="old_password" class="form-control">
                        </div>
                    </div>

                    <div class="form-group" style=" margin-top: 5px">
                        <label class="control-label col-lg-2" for="password">新密码：</label>
                        <div class="col-lg-10">
                            <input type="password" id="password" name="password" class="form-control">
                        </div>
                    </div>
                    <div class="form-group" style=" margin-top: 5px">
                        <label class="control-label col-lg-2" for="repassword">重复密码：</label>
                        <div class="col-lg-10">
                            <input type="password" id="repassword" name="repassword" class="form-control">
                        </div>
                    </div>
                </div>

            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="saveCurrentManagerPassword()"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>

<div id="loading-frame">
    <div id="loading-mask"></div>
    <div id="loading">
        <div id="loading-center">
            <div id="loading-center-absolute">
                <div class="object" id="object_four"></div>
                <div class="object" id="object_three"></div>
                <div class="object" id="object_two"></div>
                <div class="object" id="object_one"></div>
            </div>
        </div>
    </div>
</div>


<!-- error information dialog -->
<div aria-hidden="true" role="dialog" tabindex="-1" id="errorModal" class="modal fade">
    <div class="modal-dialog modal-lg modal-dialog-danger">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">错误</h4>
            </div>
            <div class="modal-body" id="errorContainer"></div>
        </div>
    </div>
</div>

<!-- warning information dialog -->
<div aria-hidden="true" role="dialog" tabindex="-1" id="warningModal" class="modal fade">
    <div class="modal-dialog modal-lg modal-dialog-warning">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">警告</h4>
            </div>
            <div class="modal-body" id="warningContainer"></div>
        </div>
    </div>
</div>

<!--confirm dialog-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="confirmModal" class="modal fade">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div id="modaledit" class="modal-body modal-del-body" style="overflow-y: auto; max-height: 774px;">
                是否确认操作？
            </div>
            <div class="modal-footer modal-del-footer">
                <a class="btn btn-primary" id="confirmOkBtn"><i class="fa fa-ok"></i> 确认</a>
                <a class="btn btn-cancel" data-dismiss="modal" id="confirmCancelBtn"><i class="fa fa-remove"></i> 取消</a>
            </div>
        </div>
    </div>
</div>

<!--alert information dialog-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="infoModal" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header modal-header-alert">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body modal-body-alert" id="infoContainer"
                 style="font-size: 14px;overflow-y: auto;max-height: 774px;">操作成功
            </div>
        </div>
    </div>
</div>


</body>
</html>